package post

import (
	"io/ioutil"
	"appengine"
	"appengine/blobstore"
)


func saveBlob(content string, ctx appengine.Context) (string, error) {
	writer, err := blobstore.Create(ctx, "text/plain")
	if err != nil {
		return "", err
	}
	_, err = writer.Write([]byte(content))
	if err != nil {
		return "", err
	}
	err = writer.Close()
	if err != nil {
		return "", err
	}
	key, _ := writer.Key()
	return string(key), nil
}

func loadBlob(key string, ctx appengine.Context) (string, error) {
	k := appengine.BlobKey(key)
	reader := blobstore.NewReader(ctx, k)

	bits, err := ioutil.ReadAll(reader)
	if err != nil {
		return "", err
	}
	return string(bits), nil
}
